from twitter.scraper import Scraper
import time
import csv
import pandas as pd
import logging
import re

from scripts.LogHandler import LogHandler


def log_analyze(log_handler):
    print(log_handler.logs)
    remain_times = re.findall(r'\d+', log_handler.logs[2])
    wait_time = (int(re.findall(r'\d+', log_handler.logs[3])[0]) + 1) * 60
    if int(remain_times[0]) == 1:
        print(gettime() + "开始等待，预计等待{}分钟".format(wait_time // 60))
        time.sleep(wait_time)
        print(gettime() + "等待结束")
    print('error')
    log_handler.logs.clear()


def gettime():
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + "|"


class IDGeter:
    def __init__(self, config_path):
        self.config_path = config_path

        self.proxy = "127.0.0.1:7890"  # 自己电脑的代理端口（要改）

        self.excel_path = r'D:\Code\程序\python\twitter\myTwitter\data\seed\RAND Corporation_seed.xlsx'

        self.filename_output = r"D:\Code\程序\python\twitter\myTwitter\data\seed\RAND Corporation_out1_id_list.csv"

        self.log_handler = LogHandler()

        self.email = ''

        self.username = ''

        self.password = ''

        self.scraper = ''

        self.screname = ''

    """读取配置文件"""

    def read_file(self):
        with open(self.config_path, "r") as fs:
            content = fs.read().strip().split('\n')
        if len(content) >= 1:
            self.email = content[0].strip()
        if len(content) >= 2:
            self.username = content[1].strip()
        if len(content) >= 3:
            self.password = content[2].strip()
        self.scraper = Scraper(self.email, self.username, self.password, debug=1, save=False)

        return self.email, self.username, self.password

    """写入CSV"""

    def write_csv(self, list_one_person, title=False):
        f = open(self.filename_output, 'a', encoding='utf-8-sig')
        header = ['id', 'twitter_name', 'twitter_username']

        writer = csv.writer(f, lineterminator='\n')
        if not title:
            writer.writerow(header)
        writer.writerow([list_one_person[0] + '\t', list_one_person[1], list_one_person[2]])

    def run(self):
        # log_handler = LogHandler()
        # self.scraper.logger.addHandler(log_handler)

        title = False

        df = pd.read_excel(self.excel_path, header=0)
        # df = df.replace(np.nan, '')
        print(df)
        df = df['twitter']

        count = 0
        for data in df:
            count += 1
            print(count)
            df = pd.DataFrame({'data': [count]})
            # 将DataFrame保存为txt文件，不保存索引列
            df.to_csv(r'D:\Code\程序\python\twitter\myTwitter\data\seed\test_count.txt', index=False, header=None)
            try:
                while True:
                    try:
                        self.screname = self.scraper.users([data])
                        break
                    except:
                        print(gettime() + str(data) + "网络异常！1s后重试")
                        time.sleep(1)
                        continue
                id = self.screname[0]['data']['user']['result']['rest_id']
                name = self.screname[0]['data']['user']['result']['legacy']['name']
                a = [id, name, data]
                self.write_csv(a, title=title)
                title = True
                print(log_handler.logs)
                remain_times = re.findall(r'\d+', log_handler.logs[2])
                wait_time = (int(re.findall(r'\d+', log_handler.logs[3])[0]) + 1) * 60
                if int(remain_times[0]) == 1:
                    print(gettime() + "开始等待，预计等待{}分钟".format(wait_time // 60))
                    time.sleep(wait_time)
                    print(gettime() + "等待结束")
                    log_handler.logs.clear()
                    continue
                log_handler.logs.clear()

            except:
                print(log_handler.logs)
                remain_times = re.findall(r'\d+', log_handler.logs[2])
                wait_time = (int(re.findall(r'\d+', log_handler.logs[3])[0]) + 1) * 60
                if int(remain_times[0]) == 1:
                    print(gettime() + "开始等待，预计等待{}分钟".format(wait_time // 60))
                    time.sleep(wait_time)
                    print(gettime() + "等待结束")
                print('error')
                log_handler.logs.clear()
                continue
